package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.ConferenceBeanStatelessLocal;
import de.hbrs.ooka.beans.PaperBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.ConferenceEntity;
import de.hbrs.ooka.entities.PaperEntity;
import de.hbrs.ooka.entities.UserEntity;
import de.hbrs.ooka.enums.Role;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PaperAddingServlet extends AbstractViewServlet {

    @EJB
    private PaperBeanStatelessLocal paperBean;
    
    @EJB
    private ConferenceBeanStatelessLocal conferenceBean;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        UserSessionStatefulLocal crsBean = this.getCrsBean(request);
        String redirection = WebPageNames.PaperAddingPage;
        RequestDispatcher rd = null;

        if (crsBean == null || !this.isUserAuthenticated(crsBean.getUser(), Role.PARTICIPANT)) {
            request.setAttribute("message", "Not logged in");
            request.setAttribute("redirect", WebPageNames.LoginViewPage);
            rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
            if (rd != null) {
                rd.forward(request, response);
            }
        } else {
            String action = request.getParameter("action");
            request.setAttribute("name", crsBean.getUser().getName());
            request.setAttribute("roles", crsBean.getUser().getRoles());
            if (action == null) {
                request.setAttribute("conference", request.getParameter("conference"));
                rd = request.getRequestDispatcher("/" + WebPageNames.PaperAddingPage + ".jsp");
                if (rd != null) {
                    rd.forward(request, response);
                }
            } else if (request.getParameter("action").equalsIgnoreCase("upload")) {
                redirection = tryUploadPaper(request.getParameter("filepath"), crsBean.getUser(), request.getParameter("conf"));
                request.removeAttribute("action");
                rd = request.getRequestDispatcher("/" + redirection);
                if (rd != null) {
                    rd.forward(request, response);
                }
            }
        }
    }

    private String tryUploadPaper(String filepath, UserEntity author, String conference) {
        PaperEntity paper = this.paperBean.checkFile(filepath, author);
        
        if (paper != null) {
            ConferenceEntity conf = this.conferenceBean.getConference(conference);
            paper.setSubmittedConference(conf);
            
            this.paperBean.uploadPaper(paper);
            
            this.conferenceBean.addPaperToConference(conf, paper);
            
            return WebPageNames.ActionSuccessfulPage;
        }

        return WebPageNames.ErrorPage + ".jsp";
    }
}
